using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EditUser_Page : BasePage
{
  string userName = "";

  protected void Page_Load(object sender, EventArgs e)
  {
     // retrieve the username from the querystring
     userName = this.Request.QueryString["UserName"];

     lblRolesFeedbackOK.Visible = false;
     lblProfileFeedbackOK.Visible = false;

     if (!this.IsPostBack)
     {
        UserProfile1.UserName = userName;

        // show the user's details
        MembershipUser user = Membership.GetUser(userName);
        lblUserName.Text = user.UserName;
        lnkEmail.Text = user.Email;
        lnkEmail.NavigateUrl = "mailto:" + user.Email;
        lblRegistered.Text = user.CreationDate.ToString("f");
        lblLastLogin.Text = user.LastLoginDate.ToString("f");
        lblLastActivity.Text = user.LastActivityDate.ToString("f");
        chkOnlineNow.Checked = user.IsOnline;
        chkApproved.Checked = user.IsApproved;
        chkLockedOut.Checked = user.IsLockedOut;
        chkLockedOut.Enabled = user.IsLockedOut;

        BindRoles();
     }
  }

  private void BindRoles()
  {
     // fill the CheckBoxList with all the available roles, and then select
     // those that the user belongs to
     chklRoles.DataSource = Roles.GetAllRoles();
     chklRoles.DataBind();
     foreach (string role in Roles.GetRolesForUser(userName))
        chklRoles.Items.FindByText(role).Selected = true;
  }

  protected void btnUpdateProfile_Click(object sender, EventArgs e)
  {
     UserProfile1.Save();
     lblProfileFeedbackOK.Visible = true;
  }

  //protected void btnCreateRole_Click(object sender, EventArgs e)
  //{
  //   if (!Roles.RoleExists(txtNewRole.Text.Trim()))
  //   {
  //      Roles.CreateRole(txtNewRole.Text.Trim());
  //      BindRoles();
  //   }
  //}

  protected void btnUpdateRoles_Click(object sender, EventArgs e)
  {
      if (chklRoles.Items.Count > 0)
      {
          // first remove the user from all roles...
          string[] currRoles = Roles.GetRolesForUser(userName);
          if (currRoles.Length > 0)
              Roles.RemoveUserFromRoles(userName, currRoles);
          // and then add the user to the selected roles
          List<string> newRoles = new List<string>();
          foreach (ListItem item in chklRoles.Items)
          {
              if (item.Selected)
                  newRoles.Add(item.Text);
          }
          Roles.AddUserToRoles(userName, newRoles.ToArray());

          lblRolesFeedbackOK.Visible = true;
      }

      Approval_Changed();
  }

  private void Approval_Changed()
  {
     MembershipUser user = Membership.GetUser(userName);
     user.IsApproved = chkApproved.Checked;
     Membership.UpdateUser(user);
     
     if(chkApproved.Checked)
        Ample.Helpers.AdminEmailUserRegisterApproval(user.UserName, user.Email, this);
  }

  protected void chkLockedOut_CheckedChanged(object sender, EventArgs e)
  {
     if (!chkLockedOut.Checked)
     {
        MembershipUser user = Membership.GetUser(userName);
        user.UnlockUser();
        chkLockedOut.Enabled = false;
     }
  }
}